package com.qqpz.sign_project.controller;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.qqpz.sign_project.entity.Admin;
import com.qqpz.sign_project.entity.Courses;
import com.qqpz.sign_project.entity.Records;
import com.qqpz.sign_project.entity.User;
import com.qqpz.sign_project.service.AdminService;
import com.qqpz.sign_project.service.CoursesService;
import com.qqpz.sign_project.service.RecordsService;
import com.qqpz.sign_project.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/v1/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;
    @Autowired
    private CoursesService coursesService;
    @Autowired
    private RecordsService recordsService;
    @Autowired
    private UserService userService;

    @PostMapping(path = "/{username}/verify")
    public String verifyAdmin(@PathVariable("username")String username,@RequestBody Map<String,Object> map1){
        String pwd=(String) map1.get("pwd");
        Admin admin=new Admin();
        admin.setPwd(pwd);
        admin.setUsername(username);
        Admin res=adminService.verifyAdmin(admin);
        Map<String,Object> map=new HashMap<>();
        if(res!=null){
            map.put("result","success");
            map.put("admin",res);
            return new Gson().toJson(map);
        }
        map.put("result","fail");
        return new Gson().toJson(map);
    }
    @GetMapping(path = "/username")
    public String queryAllUsername(){
        List<String> username = adminService.queryAllUsername();
        Map<String,Object> map=new HashMap<>();
        if(username!=null){
            map.put("result","success");
            map.put("username",username);
            return new Gson().toJson(map);
        }else{
            map.put("result","fail");
            return new Gson().toJson(map);
        }
    }
    @GetMapping(path = "/students/{username}")
    public String querystudents(@PathVariable("username")String username){
        List<Courses> coursesList = coursesService.queryByUserName(username);
        List<Records> records=new ArrayList<>();
        List<User> users=new ArrayList<>();
        for (Courses c :
                coursesList) {
            records.addAll(recordsService.queryRecordsByCourseId(c.getId()));
            for (Records r :
                    recordsService.queryRecordsByCourseId(c.getId())) {
                users.add(userService.queryUserById(r.getUserId()));
            }
        }
        Map<String,Object> map=new HashMap<>();
        if(username!=null){
            map.put("result","success");
            map.put("records",records);
            map.put("users",users);
            return new Gson().toJson(map);
        }else{
            map.put("result","fail");
            return new Gson().toJson(map);
        }
    }
    @GetMapping(path = "/")
    public String queryAllAdmin(){
        List<Admin> list = adminService.queryAllAdmin();
        Map<String,Object> map=new HashMap<>();
        if(list!=null){
            map.put("result","success");
            map.put("admin",list);
            return new Gson().toJson(map);
        }else{
            map.put("result","fail");
            return new Gson().toJson(map);
        }
    }
    @PostMapping(path = "/")
    public String createAdmin(@RequestBody Admin admin){
        int i= 0;
        try {
            i = adminService.createAdmin(admin);
        } catch (Exception e) {
            e.printStackTrace();
            i=0;
        }
        Map<String,Object> map=new HashMap<>();
        if(i!=0){
            map.put("result","success");
            return new Gson().toJson(map);
        }else{
            map.put("result","fail");
            return new Gson().toJson(map);
        }
    }
    @PostMapping(path = "/pwd")
    public String updateAdmin(@RequestBody Admin admin){
        int i= 0;
        try {
            i = adminService.updateAdmin(admin);
        } catch (Exception e) {
            e.printStackTrace();
            i=0;
        }
        Map<String,Object> map=new HashMap<>();
        if(i!=0){
            map.put("result","success");
            return new Gson().toJson(map);
        }else{
            map.put("result","fail");
            return new Gson().toJson(map);
        }
    }

    @PostMapping(path = "/name")
    public String updateAdminName(@RequestBody Admin admin){
        int i= 0;
        try {
            i = adminService.updateAdminName(admin);
        } catch (Exception e) {
            e.printStackTrace();
            i=0;
        }
        Map<String,Object> map=new HashMap<>();
        if(i!=0){
            map.put("result","success");
            return new Gson().toJson(map);
        }else{
            map.put("result","fail");
            return new Gson().toJson(map);
        }
    }

    @PostMapping(path = "/delete")
    public String deleteAdmin(@RequestBody Admin admin){
        int i= 0;
        try {
            i = adminService.deleteAdmin(admin.getUsername());
        } catch (Exception e) {
            e.printStackTrace();
            i=0;
        }
        Map<String,Object> map=new HashMap<>();
        if(i!=0){
            map.put("result","success");
            return new Gson().toJson(map);
        }else{
            map.put("result","fail");
            return new Gson().toJson(map);
        }
    }




}
